package com.ynet.eq.haiqing.common.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.Topic;


/**
 * @author 胡超雄
 * @date 2021/6/22 17:55
 * @desc ActiveMq 配置类
 */
@Configuration
public class ActiveMqConfig {

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;

//    @Bean
//    public Queue queue() {
//        return new ActiveMQQueue("ActiveMQQueue");
//    }

    @Bean
    public Topic topic(){
        return new ActiveMQTopic("ActiveMQTopic");
    }


    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(brokerUrl);
    }

    //Queue模式连接注入
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

    //Topic模式连接注入
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

    @Bean
    public BeanDemo  beanTest(){
        BeanDemo beanDemo = new BeanDemo();
        beanDemo.setDurant("杜兰特");
        beanDemo.setKobi("科比");
        beanDemo.setLem("勒布朗");
        return beanDemo;
    }



}
